package cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.log;

import cc.autoapi.pucong.auto.flow.core.flow.execNode.eventBus.PublishMsg;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.LogLevelTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.RunStatusEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class LogNodeHandlerMsgDto extends PublishMsg implements Serializable {

    private static final long serialVersionUID = 1L;

    private String apiId;

    private String instanceId;

    private String nodeId;

    private String instanceNodeId;

    private LogLevelTypeEnum logLevel;

    private String logContent;

    private RunStatusEnum runStatus;

    private Date createTime;

    private LogNodeHandlerMsgDto() {

    }

    public static LogNodeHandlerMsgDto log(String apiId, String instanceId, String nodeId, String instanceNodeId, LogLevelTypeEnum logLevel, String logContent) {
        LogNodeHandlerMsgDto msg = new LogNodeHandlerMsgDto();
        msg.setApiId(apiId);
        msg.setInstanceId(instanceId);
        msg.setNodeId(nodeId);
        msg.setInstanceNodeId(instanceNodeId);
        msg.setLogLevel(logLevel);
        msg.setLogContent(logContent);
        msg.setCreateTime(new Date());
        return msg;
    }

    public static LogNodeHandlerMsgDto start(String apiId, String instanceId, String nodeId, String instanceNodeId) {
        LogNodeHandlerMsgDto msg = new LogNodeHandlerMsgDto();
        msg.setApiId(apiId);
        msg.setRunStatus(RunStatusEnum.执行中);
        msg.setInstanceId(instanceId);
        msg.setNodeId(nodeId);
        msg.setInstanceNodeId(instanceNodeId);
        msg.setCreateTime(new Date());
        return msg;
    }

    public static LogNodeHandlerMsgDto ok(String apiId, String instanceId, String nodeId, String instanceNodeId, Boolean isIgnore) {
        LogNodeHandlerMsgDto msg = new LogNodeHandlerMsgDto();
        msg.setApiId(apiId);
        msg.setRunStatus(isIgnore ? RunStatusEnum.已跳过 : RunStatusEnum.执行成功);
        msg.setInstanceId(instanceId);
        msg.setNodeId(nodeId);
        msg.setInstanceNodeId(instanceNodeId);
        msg.setCreateTime(new Date());
        return msg;
    }

    public static LogNodeHandlerMsgDto error(String apiId, String instanceId, String nodeId, String instanceNodeId) {
        LogNodeHandlerMsgDto msg = new LogNodeHandlerMsgDto();
        msg.setApiId(apiId);
        msg.setInstanceId(instanceId);
        msg.setNodeId(nodeId);
        msg.setInstanceNodeId(instanceNodeId);
        msg.setLogLevel(LogLevelTypeEnum.ERROR);
        msg.setRunStatus(RunStatusEnum.执行失败);
        msg.setCreateTime(new Date());
        return msg;
    }
}
